package com.mingqijia.gassafety.job.strategy.impl;

import cn.hutool.core.date.DateUtil;
import com.gap.cache.redis.RedisCache;
import com.mingqijia.gassafety.db.entity.ConsumerServiceInfo;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.entity.RunningStrategy;
import com.mingqijia.gassafety.db.entity.RunningStrategyExt;
import com.mingqijia.gassafety.db.mapper.ContactsMapper;
import com.mingqijia.gassafety.job.dto.SendMessageDTO;
import com.mingqijia.gassafety.job.service.impl.CompanyServiceImpl;
import com.mingqijia.gassafety.job.service.impl.ServicePeriodStrategyServiceImpl;
import com.mingqijia.gassafety.job.strategy.PredicateStrategy;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.DtuPushTypeEnum;
import com.mingqijia.gassafety.shared.constant.MessagePushTypeEnum;
import com.mingqijia.gassafety.shared.constant.PushServicePeriodLevelEnum;
import com.mingqijia.gassafety.shared.utils.commonUtils.DateUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务到期<=30天 策略实现
 * </p>
 *
 * @author mby
 * @since 2024-01-16
 */
@Service
@Slf4j
public class UnexpiredPredicateStrategy implements PredicateStrategy { // 实现策略

    @Autowired
    RedisCache redisCache;

    @Autowired
    ContactsMapper contactsMapper;

    @Autowired
    CompanyServiceImpl companyService;

    @Autowired
    ServicePeriodStrategyServiceImpl servicePeriodStrategyService;

    /**
     * 信息发送判定，组装数据
     * @param adviceType 发送方式
     * @param data 组装数据列表
     * @param serviceList 服务信息列表
     * @param strategyMap  策略map
     * @param now 当前时间
     * @param orgName 公司名称
     * @param phone 公司电话
     */
    @Override
    public void predicateStatus(RunningStrategy strategy, List<SendMessageDTO> data, List<ConsumerServiceInfo> serviceList,
                                Map<String, RunningStrategyExt> strategyMap, Date now, String orgName, String phone) {
        if (CollectionUtils.isNotEmpty(serviceList)) {
            for (ConsumerServiceInfo service : serviceList) {
                Long snowflakeId = SnowflakeUtil.getSnowflakeId();
                Map<String, Object> cacheMap = redisCache.getCacheMap(Constants.WARNING_SERVICE_PERIOD_RUNNING + ":" + service.getImei());
                log.info("runningServicePeriodStrategy-getCacheMap:imei:{}}", service.getImei());
                if (cacheMap == null) {
                    cacheMap = new HashMap<>();
                }
                try {
                    if (strategy.getAdviceType().contains(String.valueOf(MessagePushTypeEnum.SMS.getCode()))) {
                        RunningStrategyExt ext = strategyMap.get(new StringBuilder().append(MessagePushTypeEnum.SMS.getCode())
                                .append("-").append(PushServicePeriodLevelEnum.UNEXPIRED.getCode()).toString());
                        servicePeriodStrategyService.predicateServicePeriod(MessagePushTypeEnum.SMS, PushServicePeriodLevelEnum.UNEXPIRED, service, ext, data, now, orgName, phone, snowflakeId, cacheMap, strategy);
                    }
                } catch (Exception e) {
                    log.info("runningServicePeriodStrategy-sms-UNEXPIRED-error:{}", e);
                }
                try {
                    if (strategy.getAdviceType().contains(String.valueOf(MessagePushTypeEnum.WX.getCode()))) {
                        RunningStrategyExt ext = strategyMap.get(new StringBuilder().append(MessagePushTypeEnum.WX.getCode())
                                .append("-").append(PushServicePeriodLevelEnum.UNEXPIRED.getCode()).toString());
                        servicePeriodStrategyService.predicateServicePeriod(MessagePushTypeEnum.WX, PushServicePeriodLevelEnum.UNEXPIRED, service, ext, data, now, orgName, phone, snowflakeId, cacheMap, strategy);
                    }
                } catch (Exception e) {
                    log.info("runningServicePeriodStrategy-wx-UNEXPIRED-error:{}", e);
                }
                try {
                    if (strategy.getAdviceType().contains(String.valueOf(MessagePushTypeEnum.CALL.getCode()))) {
                        RunningStrategyExt ext = strategyMap.get(new StringBuilder().append(MessagePushTypeEnum.CALL.getCode())
                                .append("-").append(PushServicePeriodLevelEnum.UNEXPIRED.getCode()).toString());
                        servicePeriodStrategyService.predicateServicePeriod(MessagePushTypeEnum.CALL, PushServicePeriodLevelEnum.UNEXPIRED, service, ext, data, now, orgName, phone, snowflakeId, cacheMap, strategy);
                    }
                } catch (Exception e) {
                    log.info("runningServicePeriodStrategy-call-UNEXPIRED-error:{}", e);
                }
            }
        }
    }

}
